package com.example.gokuplayalong.bases.mysql;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.gokuplayalong.bases.base.ExceptionHandler;
import com.example.gokuplayalong.bases.base.Tools;
import io.micrometer.common.util.StringUtils;

import java.sql.*;

public class VersionUpgrade {
     public static void version_1_0_2(String phone) {

        Connection connection = null;
        PreparedStatement psmt;
        ResultSet rs;
        JSONObject msgToClient = new JSONObject();
        msgToClient.put("result", "失败");

        try {
            connection = MysqlDruid.dataSource.getConnection();
        } catch (Throwable e) {
            ExceptionHandler.saveException(Thread.currentThread().getStackTrace()[1].getClassName() + Thread.currentThread().getStackTrace()[1].getMethodName() + Thread.currentThread().getStackTrace()[1].getLineNumber(), e);

        }
        try {

            connection.setAutoCommit(false);
            psmt = connection.prepareStatement("select * from profileinfo where phone=\"" + phone + "\"");
            rs = psmt.executeQuery();
            psmt = connection.prepareStatement("select * from  newusertask where phone=\"" + phone + "\"");
            ResultSet rsNewUser = psmt.executeQuery();
            JSONObject taskstep = new JSONObject();
            if (rs.next()) {



                if(StringUtils.isEmpty(rs.getString("invitecode"))){
                    String invitecode = Tools.getInitRandomUserId("6",6);
                    psmt = connection.prepareStatement("update profileinfo set invitecode=\""+invitecode+"\" where phone=\""+rs.getString("phone")+"\"");
                    psmt.executeUpdate();
                }


                if (rsNewUser.next() && !StringUtils.isEmpty(rsNewUser.getString("taskstep"))) {
                    taskstep = JSON.parseObject(rsNewUser.getString("taskstep"));
                }
                if (rs.getInt("isrealcertify") == 1) {
                    taskstep.put("task1", 1);
                }
                psmt = connection.prepareStatement("update newusertask set taskstep='" + taskstep.toJSONString() + "' where phone=\"" + phone + "\"");
                psmt.executeUpdate();

            }
            connection.commit();
        } catch (Throwable e) {
            try {
                connection.rollback();
            } catch (SQLException ex) {

            }
            ExceptionHandler.saveException(Thread.currentThread().getStackTrace()[1].getClassName() + Thread.currentThread().getStackTrace()[1].getMethodName() + Thread.currentThread().getStackTrace()[1].getLineNumber(), e);
            try {
                if (connection != null && !connection.isClosed()) connection.close();

            } catch (Throwable ex) {
                ExceptionHandler.saveException(Thread.currentThread().getStackTrace()[1].getClassName() + Thread.currentThread().getStackTrace()[1].getMethodName() + Thread.currentThread().getStackTrace()[1].getLineNumber(), ex);


            }
        }
        try {
            if (connection != null && !connection.isClosed()) connection.close();
        } catch (Throwable e) {
            ExceptionHandler.saveException(Thread.currentThread().getStackTrace()[1].getClassName() + Thread.currentThread().getStackTrace()[1].getMethodName() + Thread.currentThread().getStackTrace()[1].getLineNumber(), e);
        }
        System.out.println(msgToClient.toJSONString());


    }


    public static void version_1_0_3(String phone) {

        Connection connection = null;
        PreparedStatement psmt;
        ResultSet rs;


        try {
            connection = MysqlDruid.dataSource.getConnection();
        } catch (Throwable e) {
            ExceptionHandler.saveException(Thread.currentThread().getStackTrace()[1].getClassName() + Thread.currentThread().getStackTrace()[1].getMethodName() + Thread.currentThread().getStackTrace()[1].getLineNumber(), e);

        }
        try {

            connection.setAutoCommit(false);
            psmt = connection.prepareStatement("select * from profileinfo where phone=\"" + phone + "\"");
            rs = psmt.executeQuery();
            if (rs.next()) {
                psmt = connection.prepareStatement("update profileinfo set onlinetime=\""+Tools.getSDF().format(System.currentTimeMillis())+"\" where phone=\""+rs.getString("phone")+"\"");
                psmt.executeUpdate();
            }
            connection.commit();
        } catch (Throwable e) {
            try {
                connection.rollback();
            } catch (SQLException ex) {

            }
            ExceptionHandler.saveException(Thread.currentThread().getStackTrace()[1].getClassName() + Thread.currentThread().getStackTrace()[1].getMethodName() + Thread.currentThread().getStackTrace()[1].getLineNumber(), e);
            try {
                if (connection != null && !connection.isClosed()) connection.close();

            } catch (Throwable ex) {
                ExceptionHandler.saveException(Thread.currentThread().getStackTrace()[1].getClassName() + Thread.currentThread().getStackTrace()[1].getMethodName() + Thread.currentThread().getStackTrace()[1].getLineNumber(), ex);


            }
        }
        try {
            if (connection != null && !connection.isClosed()) connection.close();
        } catch (Throwable e) {
            ExceptionHandler.saveException(Thread.currentThread().getStackTrace()[1].getClassName() + Thread.currentThread().getStackTrace()[1].getMethodName() + Thread.currentThread().getStackTrace()[1].getLineNumber(), e);
        }

    }
}
